package cn.hzking.gjmall.utils;

import android.content.Context;
import android.content.SharedPreferences;

import static cn.hzking.gjmall.constant.Constants.KEY_SP_USER;


/**
 * preference工具类，默认都写入string
 * @author Administrator
 *
 */
public class SPUtils {

	public static final String TAG = "gjmall";
	public static final String SHOPID = "shopId";
	public static final String TOKEN ="token";
	private static final String SHAREURL ="shareurl" ;
	private static final String IMG_HEAD = "imghead" ;
	private static String SHOPNAME = "shopname";
	private static String SHOPDES ="shopdes";
	private static String ISSELLER = "isseller";
	private static String CARNUM = "carnum";
	private static String ADDRESSID = "addressId";
	private static String USERID = "userid";

	/**
	 * 根据name获取sp
	 * @param appContext
	 * @param name
	 * @return
	 */
	public static synchronized SharedPreferences getPreference(Context appContext, String name) {
			SharedPreferences preference =appContext.getSharedPreferences(name, Context.MODE_PRIVATE);
			return preference;
	}

	public static void clear(Context context){
		SharedPreferences.Editor edit = getPreference(context, KEY_SP_USER).edit();
		edit.clear();
		edit.commit();
	}

	/**
	 * 店铺id
	 * @param context
	 * @param shopId
	 */
	public static void putShopId(Context context, String shopId){
		getPreference(context,KEY_SP_USER).edit().putString(SHOPID,shopId).commit();
	}

	public static String getShopId(Context context){
		return getPreference(context,KEY_SP_USER).getString(SHOPID,"");
	}

	/**
	 * token值
	 * @param context
	 * @param toekn
	 */
	public static void putToken(Context context, String toekn){
		getPreference(context,KEY_SP_USER).edit().putString(TOKEN,toekn).commit();
	}

	public static String getToken(Context context){
		return getPreference(context,KEY_SP_USER).getString(TOKEN,"");
	}

	/**
	 * 分享主页的url
	 * @param context
	 * @param shareUrl
	 */
	public static  void putShareHomeUrl(Context context,String shareUrl){
		getPreference(context,KEY_SP_USER).edit().putString(SHAREURL,shareUrl).commit();
	}
	public static String getShareHomeUrl(Context context){
		return getPreference(context,KEY_SP_USER).getString(SHAREURL,"");
	}

	/**
	 * 获取头像路径
	 * @param context
	 * @return
	 */
	public static String getHeadPic(Context context){
		return getPreference(context,KEY_SP_USER).getString(IMG_HEAD,"");
	}

	public static void putHeadPic(Context context, String img){
		getPreference(context,KEY_SP_USER).edit().putString(IMG_HEAD,img).commit();
	}

	/**
	 * 店铺名称
	 * @param context
	 * @return
	 */
	public static String getShopName(Context context){
		return getPreference(context,KEY_SP_USER).getString(SHOPNAME,"");
	}

	public static void putShopName(Context context, String shopName){
		getPreference(context,KEY_SP_USER).edit().putString(SHOPNAME,shopName).commit();
	}
	/**
	 * 店铺描述
	 * @param context
	 * @return
	 */
	public static String getShopDes(Context context){
		return getPreference(context,KEY_SP_USER).getString(SHOPDES,"");
	}

	public static void putShopDes(Context context, String shopDes){
		getPreference(context,KEY_SP_USER).edit().putString(SHOPDES,shopDes).commit();
	}

	/**
	 * 是否是店主
	 * @param context
	 * @return
	 */
	public static boolean getIsSeller(Context context){
		return getPreference(context,KEY_SP_USER).getBoolean(ISSELLER,false);
	}

	public static void putIsSeller(Context context, boolean isSeller){
		getPreference(context,KEY_SP_USER).edit().putBoolean(ISSELLER,isSeller).commit();
	}

	/**
	 * 购物车数量
	 * @param context
	 * @return
	 */
	public static int getCarNum(Context context){
		return getPreference(context,KEY_SP_USER).getInt(CARNUM,0);
	}

	public static void putCarNum(Context context, int carNum){
		getPreference(context,KEY_SP_USER).edit().putInt(CARNUM,carNum).commit();
	}

	/**
	 * addressId
	 * @param context
	 * @param addressId
	 * @return
	 */
	public static void putAddressId(Context context, String addressId){
		getPreference(context,KEY_SP_USER).edit().putString(ADDRESSID,addressId).commit();
	}
	public static String getAddressId(Context context){
		return getPreference(context,KEY_SP_USER).getString(ADDRESSID,"");
	}

	/**
	 * userId
	 * @param context
	 * @param
	 * @return
	 */
	public static void putUserId(Context context, String userId){
		getPreference(context,KEY_SP_USER).edit().putString(USERID,userId).commit();
	}
	public static String getUserId(Context context){
		return getPreference(context,KEY_SP_USER).getString(USERID,"");
	}


}


